/*
 *  AMPCAlert.cpp
 *  AMPlugin
 *
 *  Created by Toan on 12/17/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */
#include "PMString.h"
#include "IApplication.h"
#include "IDialogMgr.h"

#include "CoreResTypes.h"
#include "LocaleSetting.h"
#include "RsrcSpec.h"
#include "IPanelControlData.h"
#include "ITextControlData.h"
#include "AMPCAlert.h"
#include "AMPID.h"
extern int CAlertMessage;

/********************************************************************************************************************
 *****Function: ModalAlert
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: Alert warning
 ********************************************************************************************************************/
int16 AMPCAlert::ModalAlert(const PMString& message,
						  const PMString& button1String,
						  const PMString& button2String,
						  const PMString& button3String,
						  int16 defaultWaitForDialog,
						  int16 iconID			 
						  )
{
	do
	{
		CAlertMessage = 1;
		int32	DialogResourceID;// = kAMPProgressDlgResourceID;
		if(iconID == kAMPLuginCheckInAction)
		{
			DialogResourceID = kAMPALrtCheckInDlgResourceID;
		}else
		{
			DialogResourceID = kAMPProgressDlgResourceID;
		}
		// Get the application interface and the DialogMgr.	
		InterfacePtr<IApplication> application(GetExecutionContextSession()->QueryApplication());
		if (application == nil)
		{
			ASSERT_FAIL("BscDlgActionComponent::DoDialog: application invalid");
			break;
		}
		InterfacePtr<IDialogMgr> dialogMgr(application, UseDefaultIID());
		if (dialogMgr == nil)
		{ 
			ASSERT_FAIL("BscDlgActionComponent::DoDialog: dialogMgr invalid"); 
			break;
		}
		// Load the plug-in's resource.
		PMLocaleId nLocale = LocaleSetting::GetLocale();
		RsrcSpec dialogSpec
		(
		 nLocale,					// Locale index from PMLocaleIDs.h. 
		 kAMPPluginID,				// Our Plug-in ID from BasicDialogID.h. 
		 kViewRsrcType,				// This is the kViewRsrcType.
		 DialogResourceID,			// Resource ID for our dialog.
		 kTrue						// Initially visible.
		 );
		
		// CreateNewDialog takes the dialogSpec created above, and also
		// the type of dialog being created (kMovableModal).
		IDialog* dialog = dialogMgr->CreateNewDialog(dialogSpec, IDialog::kMovableModal);
		if (dialog == nil)
		{ 
			ASSERT_FAIL("BscDlgActionComponent::DoDialog: can't create dialog"); 
			break;
		}

		InterfacePtr<IPanelControlData> iPanelControlData(dialog, UseDefaultIID());
		ASSERT(iPanelControlData);
		if(iPanelControlData==nil) {
			break;
		}
		
		IControlView* AlertView = iPanelControlData->FindWidget(kAMPCAlertMessageWidgetID);
		if(!AlertView)
		{
			break;
		}
		
		InterfacePtr<ITextControlData> MessageWarning(AlertView, UseDefaultIID());
		if(!MessageWarning)
		{
			break;
		}
		MessageWarning->SetString(message);
		IControlView* Button1View = iPanelControlData->FindWidget(kOKButtonWidgetID);
		if(!Button1View)
		{
			break;
		}
		IControlView* Button2View = iPanelControlData->FindWidget(kAMPCAlertButton1WidgetID);
		if(!Button1View)
		{
			break;
		}
		Button1View->Hilite(kTrue);
		IControlView* Button3View = iPanelControlData->FindWidget(kAMPCAlertButton2WidgetID);
		if(!Button1View)
		{
			break;
		}
		InterfacePtr<ITextControlData> Btt_1_message(Button1View, UseDefaultIID());
		if(!Btt_1_message)
		{
			break;
		}
		Btt_1_message->SetString(button1String);
		if(button2String == kNullString){
			Button2View->Hide();
		}else {
			Button2View->Show();
			InterfacePtr<ITextControlData> Btt_2_message(Button2View, UseDefaultIID());
			if(!Btt_2_message)
			{
				break;
			}
			
			Btt_2_message->SetString(button2String);
		}
		if(button3String == kNullString){
			Button3View->Hide();
		}else {
			Button3View->Show();
			InterfacePtr<ITextControlData> Btt_3_message(Button3View, UseDefaultIID());
			if(!Btt_3_message)
			{
				break;
			}
			Btt_3_message->SetString(button3String);
		}
		
		dialog->Open();
		
		if(defaultWaitForDialog == kNeedToWaitForDialog) {
			dialog->WaitForDialog();
		}
		//cout << "dialog close:" << CAlertMessage << endl;	
	} while (false);
	return CAlertMessage;
}